package cn.bxshey.modules_order.controller;


import cn.bxshey.feign.api.UserClient;
import cn.bxshey.general.Result.Result;
import cn.bxshey.general.aop.note.Permissions;
import cn.bxshey.general.entity.Order;
import cn.bxshey.modules_order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单服务
 */

@RestController
@RequestMapping("/order")
@Transactional
@RefreshScope
public class OrderController {

    @Resource
    UserClient userClient;

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 订单服务测试
     * @return String
     */

    @GetMapping("/test")
    public Result<String> testDemo() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            System.out.println(request.getHeader("feign-token"));
            System.out.println(request.getHeader("Authorization"));
        }
//        System.err.println(userClient.testDemo().getData());
        return Result.success("订单服务 测试成功！");
    }

    //获取订单所有信息
    @GetMapping("/selectAll")
    @Permissions(url = "群星")
    public Result<List<Order>> getAll() {
        return Result.success(orderService.selectGetAll());
    }

    //根据用户id获取用户所有订单信息
    @GetMapping("/selectByUserId/{id}")
    public Result<List<Order>> getByID(@PathVariable("id") Long id) {
        return Result.success(orderService.selectByUserId(id));
    }

    //根据订单id获取订单信息
    @Permissions(url = "群星")
    @GetMapping("/authentication/selectById/{id}")
    public Result<Order> selectById(@PathVariable("id") Long id) {
        return Result.success(orderService.selectById(id));
    }
}
